﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars.Ribbon;
using Sunboxsoft.WinForm.Shell.Components;
using DevExpress.LookAndFeel;
using DevExpress.XtraBars;

namespace Sunboxsoft.WinForm.Shell
{
    public partial class MainForm : RibbonForm, IViewContainer
    {
        ApplicationMenu _applicationMenu;

        private MaskLayer _ctlMaskLayer;
        private int _maskLayerRetain;

        private IModule _currentModule;

        public MainForm(IModule module)
        {
            InitializeComponent();

            _applicationMenu = new ApplicationMenu();
            _applicationMenu.MinWidth = 230;
            _applicationMenu.MenuDrawMode = DevExpress.XtraBars.MenuDrawMode.SmallImagesText;
            _applicationMenu.Ribbon = this.ctlRibbonControl;
            this.ctlRibbonControl.ApplicationButtonDropDownControl = _applicationMenu;

            _currentModule = module;
            ActiveModule();

            DefaultLookAndFeel defaultLookAndFeel = new DefaultLookAndFeel();
            defaultLookAndFeel.LookAndFeel.SkinName = "Office 2010 Silver";
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            UIContext.Current.RegisterManager(new ViewManager(this, ctlTxtTitle, mainContent1),
                new NavigatorManager(this.ctlRibbonControl, _applicationMenu, ctlRibbonStatusBar, ctlSplitContainerControl, ctlNavBarControl));
            UIContext.Current.NavigatorManager.ShowPrimaryActions(UIContext.Current.ModuleRuntimeContext.Roots);
            UIContext.Current.NavigatorManager.ShowMenuActions(UIContext.Current.ModuleRuntimeContext.Menus);
            UIContext.Current.NavigatorManager.ShowStatusActions(UIContext.Current.ModuleRuntimeContext.StatusBarLeftActions.Roots,
                UIContext.Current.ModuleRuntimeContext.StatusBarRightActions.Roots);

            if (UIContext.Current.ModuleRuntimeContext.DefaultActionNode != null)
            {
                UIContext.Current.ActiveActionNode(UIContext.Current.ModuleRuntimeContext.DefaultActionNode);
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (!UIContext.Current.ViewManager.ShowQuestion(string.Format("是否要退出\"{0}\"?", _currentModule.Name)))
            {
                e.Cancel = true;
            }
            base.OnClosing(e);
        }

        #region IViewContainer 成员

        public void ShowMask(string text)
        {
            this.BeginInvoke(new Action(() =>
            {
                if (_ctlMaskLayer != null)
                {
                    _ctlMaskLayer.SetText(text);
                }
                else
                {
                    _ctlMaskLayer = new MaskLayer();
                    _ctlMaskLayer.Dock = DockStyle.Fill;
                    this.Controls.Add(_ctlMaskLayer);
                    _ctlMaskLayer.BringToFront();
                    _ctlMaskLayer.SetText(text);
                }
                _maskLayerRetain++;
            }));
        }

        public void HideMask()
        {
            this.BeginInvoke(new Action(() =>
            {
                _maskLayerRetain--;
                if (_ctlMaskLayer != null)
                {
                    if (_maskLayerRetain == 0)
                    {
                        this.Controls.Remove(_ctlMaskLayer);
                        _ctlMaskLayer = null;
                    }
                }
            }));
        }

        public void ShowAction(string name, bool isShow)
        {
            Bar bar = this.mainContent1.GetToolbar();
            BarManager barManager = bar.Manager;
            for (int index = 0; index < barManager.Items.Count; index++)
            {
                if (string.Equals(barManager.Items[index].Name, name))
                {
                    if (isShow)
                    {
                        barManager.Items[index].Visibility = BarItemVisibility.Always;
                    }
                    else
                    {
                        barManager.Items[index].Visibility = BarItemVisibility.Never;
                    }
                }
            }
        }

        public void EnableAction(string name, bool isEnable)
        {
            Bar bar = this.mainContent1.GetToolbar();
            BarManager barManager = bar.Manager;
            for (int index = 0; index < barManager.Items.Count; index++)
            {
                if (string.Equals(barManager.Items[index].Name, name))
                {
                    barManager.Items[index].Enabled = isEnable;
                }
            }
        }

        #endregion

        private void ActiveModule()
        {
            this.Text = string.Format("{0}-v{1}", _currentModule.Name, this.GetType().Assembly.GetName().Version.ToString(4));
            ctlRibbonControl.ApplicationIcon = new Bitmap(_currentModule.Image, new Size(32,32));
        }

        public void ShowDebugText(string text)
        {
//#if DEBUG
//            this.BeginInvoke(new Action(() =>
//            {
//                ctlLabelDebug.Text = text;
//            }));
//#endif
        }
    }
}
